package com.wuchao.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.mysql.cj.xdevapi.JsonArray;
import com.wuchao.entity.User;
import com.wuchao.service.serviceImpl.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author wuchao66
 * @Date 2022/7/5 14:19
 * @Version 1.0
 */
@WebServlet(name = "ServletAjax",urlPatterns = "/ServletAjax")
//注解 标注解释说明
public class ServletAjax extends HttpServlet {//中转页的协议
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();//文件流

        out.flush();
        out.close();
    }
//默认get
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
       // HttpSession session = (HttpSession)request.getSession();
        PrintWriter out = response.getWriter();//文件流
        String op = request.getParameter("op");
        ServiceImpl service = new ServiceImpl();
        if(op!=null) {
            if (op.equals("register")) {
                String name = request.getParameter("name");
                int loading = service.isLoading("select count(1) from user where name=?", name);
                if (loading > 0) {//这里不能用request
                    out.print("yes");//异步回去，就这个方法
                } else {
                    out.print("no");
                }

            } else if (op.equals("register1")) {
                String name = request.getParameter("name");
                int loading = service.isLoading("select count(1) from user where name=?", name);
                if (loading > 0) {//这里不能用request
                    out.print("yes");//异步回去，就这个方法
                } else {
                    out.print("no");
                }

            } else if (op.equals("register2")) {
                String name = request.getParameter("name");
                int loading = service.isLoading("select count(1) from user where name=?", name);
                if (loading > 0) {//这里不能用request
                    out.print("yes");//异步回去，就这个方法
                } else {
                    out.print("no");
                }

            } else if (op.equals("find")) {
                String name = request.getParameter("name");
                List<User> list = service.findPoJo("select * from user where name like ?", "%" + name + "%");
                //String json3=[{"name":"超哥","age":33},{"name":"蝴蝶","age":33},{"name":"波波","age":34}];
                StringBuffer sb = new StringBuffer("[");
                for (int i = 0; i < list.size(); i++) {//核心：无用打包
                    String json = "{\"id\":\"" + list.get(i).getId() + "\",\"name\":\"" + list.get(i).getName() + "\"}";
                    sb.append(json);
                    if (i < list.size() - 1) {
                        sb.append(",");
                    }
                }
                sb.append("]");
                System.out.println(sb);
                out.print(sb);

            }else if (op.equals("findJson")) {
                String name = request.getParameter("name");
                List<User> list = service.findPoJo("select * from user where name like ?", "%" + name + "%");
                //json序列化，一句话
                String s = JSON.toJSONString(list, SerializerFeature.PrettyFormat,SerializerFeature.WriteMapNullValue);
                System.out.println(s);
                out.print(s);

            } else if (op.equals("find1")) {
                String name = request.getParameter("name");
                int loading = service.isLoading("select count(1) from user where name=?", name);
                if (loading > 0) {//这里不能用request
                    out.print("yes");//异步回去，就这个方法
                } else {
                    out.print("no");
                }


            }else if (op.equals("f")) {
                String name = request.getParameter("name");
                String hobby = request.getParameter("pwd");
                int loading = service.update("insert user(name,sex,hobby) values(?,?,?)", name,1,hobby);
                if (loading > 0) {//这里不能用request
                    out.print("yes");//异步回去，就这个方法
                } else {
                    out.print("no");
                }


            }

            out.flush();
            out.close();
        }
    }
}
